# @Time    : 2022/8/22 18:51
# @Author  : 南黎
# @FileName: 11.关闭窗口.py
import win32api
import win32con
import win32gui

# 注意窗口句柄获取如果失败，要做好异常处理
try:
    # 获取窗口句柄
    handle = win32gui.FindWindowEx(0, 0, "Notepad", None)#桌面窗口的所有子窗口检索类名"Edit"，标题为None的窗口
    print("窗口句柄是：{}".format(handle))
    result=win32api.MessageBox(handle, "我要关闭句柄为{}的窗口了".format(handle), "关闭窗口警告", 1)
    if result == 1:
        win32api.MessageBox(handle, "关闭窗口了", "关闭窗口", 0)
        #关闭窗口
        print(win32gui.PostMessage(handle, win32con.WM_CLOSE, 0, 0))
    elif result == 2:
        win32api.MessageBox(handle, "看来你不想关闭窗口啊", "不关闭窗口", 0)

except Exception as e:
    print("窗口句柄获取失败或是前台设置失败：{}".format(e))